package com.ruoyi.project.cms.dept.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.cms.dept.domain.MDept;
import com.ruoyi.project.cms.dept.service.MDeptService;
import com.ruoyi.project.system.dept.domain.Dept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司:
 * 作者:胖虎
 * 类名:DeptController
 * 描述:
 * 时间:2020-05-18
 */
@Controller
@RequestMapping("/cms/dept")
public class MDeptController   extends BaseController {

    private   String prefix = "cms/dept";

    @Autowired
    private   MDeptService  deptService;

    /**
     * 菜单跳转
     * @return
     */
    @GetMapping()
    public String dept(/*ModelMap mmap*/)
    {
        /*List<MDept>  deptList = deptService.selectDeptList(new  MDept());
        mmap.put("dept", deptList);*/
        return prefix + "/dept";
    }

    /**
     * 查询部门
     * @param dept
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public  List<MDept>   list(MDept dept)
    {
        //startPage();
        List<MDept>  deptList = deptService.selectDeptList(dept);
        //System.out.println(deptList);

        return    deptList;
    }

    /**
     * 部门添加页面的跳转
     * @return
     */
    @GetMapping("/add")
    public String add()
    {

        return prefix + "/add";
    }

    /**
     * 检验部门名字不可以相同
     * @param dept
     * @return
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(MDept dept)
    {
        return deptService.checkDeptNameUnique(dept);
    }

    /**
     * 修改页面的跳转 ,同时伴随着数据
     */
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        MDept dept = deptService.selectDeptById(deptId);
        System.out.println(dept);

        mmap.put("dept", dept);
        return   prefix + "/edit";
    }



    
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated MDept dept)
    {
        /*if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return  error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }*/

        return toAjax(deptService.updateDept(dept));
    }


    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId)
    {

        if (deptService.checkDeptExistUser(deptId))
        {
            return AjaxResult.warn("部门存在用户,不允许删除");
        }
        return   toAjax(deptService.deleteDeptById(deptId));
    }

}
